package com.taxinfo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxinfo.pojo.dto.LoginDto;
import com.taxinfo.dao.LoginMapper;
import com.taxinfo.pojo.vo.LoginInfo;
import com.taxinfo.service.LoginService;
import com.taxinfo.utils.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author : wanghy
 * @time : 2020-09-01 13:04
 */
@Service("LoginServiceImpl")
public class LoginServiceImpl extends ServiceImpl<LoginMapper, LoginInfo>  implements LoginService {

    @Value("${spring.mail.username}")
    private String SEND_USER_ADDR;

    @Value("${spring.mail.password}")
    private String SEND_PASSWORD_ADDR;

    @Value("${spring.mail.from}")
    private String FROM;

    @Value("${spring.mail.host}")
    private String HOST;

    @Value("${spring.mail.port}")
    private String PORT;

    @Value("${url.checkNode}")
    private String requestUrl;


    private LoginMapper loginMapper;

    @Override
    public LoginDto verify(String username, String password) {
        return loginMapper.verifySelect(username, password);
    }

    @Override
    public LoginDto getUserInfo(String username) {
        return loginMapper.getUserInfo(username);
    }

    @Autowired
    public void setLoginMapper(LoginMapper loginMapper) {
        this.loginMapper = loginMapper;
    }



    /**
     *
     * @param es_receiver
     * @param es_title
     * @param es_content
     */
    @Override
    public boolean sendMailMsg(String es_receiver, String es_title, String es_content){
        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", HOST);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.port", PORT);
        properties.put("maidl.smtp.socketFactory.port", PORT);
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.connecttimeout", "5000");
        properties.put("mail.smtp.timeout", "5000");
        // 获取默认session对象
        Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(SEND_USER_ADDR, SEND_PASSWORD_ADDR); // 根据你的邮箱账号和密码填写，注意安全存储密码或使用应用专用密码（例如Gmail的App Password）
            }
        });

        try {
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);
            // Set From: 头部头字段
            message.setFrom(new InternetAddress(FROM));
            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(es_receiver));
            // Set Subject: 头部头字段
            message.setSubject(es_title);
            // 设置消息体
            message.setText(es_content);
            // 发送消息
            Transport.send(message);

            return true;
        } catch (MessagingException mex) {
            mex.printStackTrace();
            return false;
        }
    }

    @Override
    public int updateUser(String verifyCode,String username) {
        return loginMapper.updateUser(verifyCode,username);
    }

    @Override
    public boolean verifyCode(String code, String username) {
        LoginDto loginDto =  loginMapper.verifyCode(code,username);
        if(loginDto == null){
            return false;
        }else{
            return true;
        }

    }

    @Override
    public void updatePassword(String password, String username) {
        loginMapper.updatePassword(password,username);
    }

    @Override
    public void updateBaseDate(String baseDate,String username) {
        loginMapper.updateBaseDate(baseDate,username);
    }

    @Override
    public String getCertfyInfo(HashMap<String, Object> request) throws Exception {
        return  HttpClientUtil.get(requestUrl,request);
    }

    @Override
    public void updateSetting(String ip, String token,String username) {
        loginMapper.updateSetting(ip, token,username);
    }

}
